package com.android.kmlmrg.model;

import java.io.File;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.os.Environment;
import android.text.format.DateFormat;

import com.android.kmlmrg.event.UserFieldChangedEvent;
import com.android.kmlmrg.listener.custom.UserFieldListener;
import com.android.kmlmrg.services.ServiceController;
import com.android.kmlmrg.utils.Const;


@SuppressWarnings("serial")
public class User  implements Serializable,Const{

	private String timesTamp ;

	private String hash ;

	private String wsCallerId ;

	private String identifiant ;

	private String nom;

	private String prenom ;

	private String dateNaissance;

	private String numeroRue;

	private String nomRue;

	private String ville;

	private String codePostal ;

	private String pays ;

	private String tel ;

	private String email ;

	private String photoURL ;

	private String pathExternalStorage ;

	private static ArrayList<UserFieldListener> listeners = new ArrayList<UserFieldListener>();

	public User(){
		this.nom=STRING_EMPTY;
		this.prenom=STRING_EMPTY;
		this.dateNaissance=STRING_EMPTY;
		this.numeroRue=STRING_EMPTY;
		this.nomRue=STRING_EMPTY;
		this.ville=STRING_EMPTY;
		this.codePostal=STRING_EMPTY;
		this.pays=STRING_EMPTY;
		this.tel=STRING_EMPTY;
		this.email=STRING_EMPTY;
	}

	public String getTimesTamp() {
		return timesTamp;
	}


	public void setTimesTamp(String timesTamp) {
		this.timesTamp = timesTamp;
	}


	public String getHash() {
		return hash;
	}


	public void setHash(String hash) {
		this.hash = hash;
	}



	public String getWsCallerId() {
		return wsCallerId;
	}


	public void setWsCallerId(String wsCallerId) {
		this.wsCallerId = wsCallerId;
	}



	public String getNom() {
		return nom;
	}



	public void setNom(String pNom) {
//		if (!compareField(nom, pNom)) return;
		fieldChanged( nom, "nom");
		this.nom = pNom;
	}



	public String getPrenom() {
		return prenom;
	}


	public void setPrenom(String Pprenom) {
//		if (!compareField(prenom, Pprenom)) return;
		fieldChanged( prenom, "prenom");
		this.prenom = Pprenom;
	}


	public String getDateNaissance() {
		return dateNaissance;
	}



	public void setDateNaissance(String pDateNaissance) {
//		if (!compareField(dateNaissance, pDateNaissance)) return;
		fieldChanged( dateNaissance, "dateNaissance");
		this.dateNaissance = pDateNaissance;
	}

	public void setDateNaissance(Date pDateNaissance) {
		CharSequence vDate = DateFormat.format("dd/MM/yyyy", pDateNaissance);		
		fieldChanged( dateNaissance, "dateNaissance");
		this.dateNaissance = vDate.toString();
	}

	public String getNumeroRue() {
		return numeroRue;
	}



	public void setNumeroRue(String pNumeroRue) {
//		if (!compareField(numeroRue, pNumeroRue)) return;
		fieldChanged( numeroRue, "numeroRue");
		this.numeroRue = pNumeroRue;
	}



	public String getNomRue() {
		return nomRue;
	}



	public void setNomRue(String pNomRue) {
//		if (!compareField(nomRue, pNomRue)) return;
		fieldChanged( nomRue, "nomRue");
		this.nomRue = pNomRue;
	}



	public String getVille() {
		return ville;
	}



	public void setVille(String pVille) {
//		if (!compareField(ville, pVille)) return;
		fieldChanged( ville, "ville");
		this.ville = pVille;
	}



	public String getCodePostal() {
		return codePostal;
	}



	public void setCodePostal(String pCodePostal) {
//		if (!compareField(codePostal, pCodePostal)) return;
		fieldChanged( codePostal, "codePostal");
		this.codePostal = pCodePostal;
	}



	public String getPays() {
		return pays;
	}



	public void setPays(String pPays) {
//		if (!compareField(pays, pPays)) return;
		fieldChanged( pays, "pays");
		this.pays = pPays;
	}



	public String getTel() {
		return tel;
	}



	public void setTel(String pTel) {
//		if (!compareField(tel, pTel)) return;
		fieldChanged( tel, "tel");
		this.tel = pTel;
	}



	public String getEmail() {
		return email;
	}



	public void setEmail(String pEmail) {
//		if (!compareField(email, pEmail)) return;
		fieldChanged( email, "email");
		this.email = pEmail;
	}



	public String getPhotoURL() {
		return photoURL;
	}



	public void setPhotoURL(String pPhotoURL) {
//		if (!compareField(photoURL, pPhotoURL)) return;
		fieldChanged( photoURL, "photoURL");
		this.photoURL = pPhotoURL;
	}



	public String getIdentifiant() {
		return identifiant;
	}



	public void setIdentifiant(String pIdentifiant) {
		fieldChanged(identifiant, "identifiant");
		this.identifiant = pIdentifiant;

	}



	public void setInfoSession ( ApiSession pApiSession ) {
		this.setWsCallerId(pApiSession.getWsCallerId());
		this.setHash(pApiSession.getHash());
		this.setTimesTamp(pApiSession.getTimesTamp());
	}

	public String toData () throws JSONException {
		ServiceController vServiceController = ServiceController.getInstance();
		JSONObject vJsonObject = new JSONObject();

		if (vServiceController != null ) {
			ApiSession vApiSession = vServiceController.getApiSession();			
			vJsonObject.put(ApiSession.WSCALLERID, vApiSession.getWsCallerId());
			vJsonObject.put(ApiSession.TIMESTAMP, vApiSession.getTimesTamp());
			vJsonObject.put(ApiSession.HASH, vApiSession.getHash());
			vJsonObject.put("nom", nom);
			vJsonObject.put("prenom", prenom);
			vJsonObject.put("dateNaissance", dateNaissance);
			vJsonObject.put("numeroRue", numeroRue.toString());
			vJsonObject.put("nomRue", nomRue);
			vJsonObject.put("ville", ville);
			vJsonObject.put("codePostal", codePostal.toString());
			vJsonObject.put("pays", pays);
			vJsonObject.put("email", email);
			vJsonObject.put("tel", tel);
			//vJsonObject.put("geolocalisable", geolocalisable);
		}
		return vJsonObject.toString();
	}
	
	
	public String toDataForUpdate () throws JSONException {
		ServiceController vServiceController = ServiceController.getInstance();
		JSONObject vJsonObject = new JSONObject();

		if (vServiceController != null ) {
			ApiSession vApiSession = vServiceController.getApiSession();			
			vJsonObject.put(ApiSession.WSCALLERID, vApiSession.getWsCallerId());
			vJsonObject.put(ApiSession.TIMESTAMP, vApiSession.getTimesTamp());
			vJsonObject.put(ApiSession.HASH, vApiSession.getHash());
			vJsonObject.put("identifiant", identifiant);
			vJsonObject.put("nom", nom);
			vJsonObject.put("prenom", prenom);
			vJsonObject.put("dateNaissance", dateNaissance);
			vJsonObject.put("numeroRue", numeroRue.toString());
			vJsonObject.put("nomRue", nomRue);
			vJsonObject.put("ville", ville);
			vJsonObject.put("codePostal", codePostal.toString());
			vJsonObject.put("pays", pays);
			vJsonObject.put("email", email);
			vJsonObject.put("tel", tel);			
			//String vStringStream = MethodeFile.getStringByteJpegFromFile( pathExternalStorage );
			//vJsonObject.put("photoStream", vStringStream);

		}
		return vJsonObject.toString();
	}
	
	

	@Override
	public String toString() {

		return identifiant + " " + nom + " " + prenom + " " + photoURL + " " ;
	}

	private void fieldChanged (Object pOldValue, String pFielName) {
		ServiceController serviceController = ServiceController.getInstance();
		Users vUsers = serviceController.getUsers();
		if ( vUsers.getList().contains(this) ){
			UserFieldChangedEvent vEvent = new UserFieldChangedEvent(this, pOldValue,  pFielName);
			fireFielUser(vEvent);
		}
	}

	public void fireFielUser( UserFieldChangedEvent pUserFieldChangedEvent ){		
		for(UserFieldListener listener : listeners){
			listener.userFielChanged(pUserFieldChangedEvent);
		}
	}


	public static void addListener ( UserFieldListener pListener ){
		listeners.add(pListener);
	}

	public static void removeListener( UserFieldListener pListener ){
		listeners.remove(pListener);
	}


	public void setImage(Bitmap pImage) {
		if (pImage != null ) {
			//fieldChanged( image, "image");
			if (saveImgUnderExternalStorage(pImage)){
				//pathExternalStorage = "Image-"+ getIdentifiant() +".jpg";
			}
		}
	}


	public String getPathExternalStorage() {
		return pathExternalStorage;
	}


	private void setPathExternalStorage(String pathExternalStorage) {
		fieldChanged( pathExternalStorage, "pathExternalStorage");
		this.pathExternalStorage = pathExternalStorage;
	}



	private boolean saveImgUnderExternalStorage (Bitmap pBitmap) {
	
		String root = Environment.getExternalStorageDirectory().toString();
		File myDir = new File(root + "/saved_images_mlkmrg");    
		if (!myDir.exists()){
			myDir.mkdir();
		}

		String fname = "Image-"+ getIdentifiant() +".jpg";
		File file = new File (myDir, fname);
		if (file.exists ()) file.delete (); 
		try {
			FileOutputStream out = new FileOutputStream(file);
			pBitmap.compress(Bitmap.CompressFormat.JPEG, 10, out);
			out.flush();
			out.close();
			setPathExternalStorage( file.getAbsolutePath() );

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	private boolean compareField (String f1, String f2) {
		if (STRING_EMPTY.equals(f1)  || STRING_EMPTY.equals(f2))return true;
		return f2.equalsIgnoreCase(f1);
	}


	

}
